package service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import ejb.PositionCommentEJBRemote;
import entityJPA.Positioncomment;

/**
 * Session Bean implementation class PositionCommentService
 */
@Stateless
@LocalBean
public class PositionCommentService {
	@EJB
	PositionCommentEJBRemote positionCommentEJB;

	private static Logger logger = Logger
			.getLogger(UserService.class.getName());

	
	/**
	 * Default constructor.
	 */
	public PositionCommentService() {
		FileHandler fh;
		try {
			fh = new FileHandler("C:/PositionCommentLoggerFile.log", true);
			SimpleFormatter formatter = new SimpleFormatter();
			fh.setFormatter(formatter);
			logger.addHandler(fh);
		} catch (SecurityException | IOException e) {
			e.printStackTrace();
		}	}

	public void insert(Positioncomment positioncomment) throws Exception {
		try {
			positionCommentEJB.persistPositioncomment(positioncomment);
			logger.info("inserted user:" + positioncomment.toString());
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage());
		}
	}

	public void update(Positioncomment positioncomment) throws Exception {
		try {
			positionCommentEJB.mergePositioncomment(positioncomment);
			logger.info("inserted user:" + positioncomment.toString());
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage());
		}
	}

	public void delete(Positioncomment positioncomment) throws Exception {
		try {
			positionCommentEJB.removePositioncomment(positioncomment);
			logger.info("inserted user:" + positioncomment.toString());
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage());
		}
	}

	public List<Positioncomment> findAllPositionComment() {
		return positionCommentEJB.getPositioncommentFindAll();
	}

	public Positioncomment findPositionCommentById(int idPositionComment) {
		for (Positioncomment pc : findAllPositionComment())
			if (idPositionComment == pc.getIdPositionComment())
				return pc;
		return null;
	}

	public List<Positioncomment> findAllCommentsByPosition(int idPosition) {
		List<Positioncomment> pcList = new ArrayList<Positioncomment>();
		for (Positioncomment pc : findAllPositionComment())
			if (idPosition == pc.getPosition().getIdPosition())
				pcList.add(pc);
		return pcList;

	}
}
